package com.begye.smartloan.apptest.renzheng.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.GridLayoutManager
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaFillItemBangKaListBinding
import com.begye.smartloan.apptest.ext.addColor
import com.begye.smartloan.apptest.renzheng.adapter.FillItemBangKaListDialogAdapter
import com.begye.smartloan.apptest.tool.addTouch
import org.json.JSONArray
import org.json.JSONObject

class FillItemBangKaListDialogFragment : DialogFragment() {

    private var _binding: DiaFillItemBangKaListBinding? = null
    private val binding get() = _binding!!
    private var _clickListener: (String, HashMap<String, Any>) -> Unit = { _, _ -> }

    private lateinit var _adapter: FillItemBangKaListDialogAdapter

    private val _dataSource: MutableList<JSONObject> = mutableListOf()
    private var _title: String? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val layout = inflater.inflate(R.layout.dia_fill_item_bang_ka_list, container, false)
        _binding = DiaFillItemBangKaListBinding.bind(layout)
        return layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        _adapter = FillItemBangKaListDialogAdapter(emptyList()) { position, _ ->
            for (index in 0 until _adapter.dataList.size) {
                val obj = _adapter.dataList[index]
                obj.put("checked", index == position)
            }
            _adapter.notifyDataSetChanged()
        }
        binding.diaFillItemBangKaList.layoutManager = GridLayoutManager(context, 2)
        binding.diaFillItemBangKaList.adapter = _adapter

        binding.diaFillItemBangKaListConfirm.setOnClickListener {
            var channel = ""
            for (index in 0 until _adapter.dataList.size) {
                val obj = _adapter.dataList[index]
                val checked = obj.optBoolean("checked", false)
                if (checked) {
                    channel = obj.optString("value", "")
                    break
                }
            }

            if (channel.isBlank()) {
                return@setOnClickListener
            }

            val mobile1 = binding.diaFillItemBangKaListMobileEdit1.text.toString().trim()
            val mobile2 = binding.diaFillItemBangKaListMobileEdit2.text.toString().trim()

            if (mobile1.isBlank()) {
                binding.diaFillItemBangKaListMobileEdit1.setBackgroundResource(R.drawable.layer_c25_fafafa_err)
                return@setOnClickListener
            }
            if (mobile2.isBlank()) {
                binding.diaFillItemBangKaListMobileEdit2.setBackgroundResource(R.drawable.layer_c25_fafafa_err)
                return@setOnClickListener
            }

            if (mobile1 != mobile2) {
                binding.diaFillItemBangKaListMobileEdit1.setBackgroundResource(R.drawable.layer_c25_fafafa_err)
                binding.diaFillItemBangKaListMobileEdit2.setBackgroundResource(R.drawable.layer_c25_fafafa_err)
                return@setOnClickListener
            }

            val params = hashMapOf<String, Any>().apply {
                put("walletMobile", mobile1)
                put("walletMobileConfirm", mobile1)
                put("walletChannel", channel)
            }
            _clickListener.invoke(this.tag.toString(), params)

            dismiss()
        }

        binding.diaFillItemBangKaListMobileEdit1.addBr {
            calBtw("bang_ka_number", it)
        }

        binding.diaFillItemBangKaListMobileEdit2.addBr {
            calBtw("bang_ka_number_confirm", it)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return

        window.attributes.gravity = Gravity.BOTTOM
        window.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        binding.diaFillItemBangKaListTitle.text = _title
        binding.diaFillItemBangKaListTitle.addColor("*", "#F55555")

        _adapter.dataList = _dataSource
        _adapter.notifyDataSetChanged()

        //
        addTouch(type = "bang_ka_list_dialog_show")
    }

    fun addTitle(value: String): FillItemBangKaListDialogFragment {
        _title = value
        return this
    }

    fun addDataSource(value: JSONArray): FillItemBangKaListDialogFragment {
        _dataSource.clear()
        for (index in 0 until value.length()) {
            val obj = value.optJSONObject(index)
            if (obj != null) {
                _dataSource.add(obj)
            }
        }
        return this
    }

    fun addClickListener(listener: (String, HashMap<String, Any>) -> Unit): FillItemBangKaListDialogFragment {
        _clickListener = listener
        return this
    }

    private fun calBtw(calTag: String, value: String) {
        try {
            addTouch(type = "INPUT_$calTag", remark = value)

        } catch (e: Exception) {
            //
        }
    }
}